<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" style="width: 100%" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="所属字典" prop="parentId">
				<el-select v-model="form.parentId" style="width: 100%" :disabled="true">
					<el-option v-for="item in dicList" :key="item.dicId" :value="item.dicId" :label="item.name"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="项名称" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="键值" prop="keyValue">
				<el-input v-model="form.keyValue" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增项',
					edit: '编辑项'
				},
				visible: false,
				isSaveing: false,
				form: {
					sortNo:1,
					dicId: "",
					code:"",
					parentId: "",
					name: "",
					keyValue: "",
					status: "1"
				},
				rules: {
					parentId: [
						{required: true, message: '请选择所属字典'}
					],
					name: [
						{required: true, message: '请输入项名称'}
					],
					keyValue: [
						{required: true, message: '请输入键值'}
					]
				},
				dicList: [],
			}
		},
		mounted() {
			if(this.params){
				this.form.code = this.params.code
			}
			this.getProDicTree()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//获取字典列表
			async getProDicTree(){
				var res = await this.$API.project.proDic.getProDicTree.get();
				if(res.code==200)
				{
					this.dicList = res.data;
				}
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if(this.mode=="add")
						{
							res = await this.$API.project.proDic.insert.post(this.form);
						}else
						{
							res = await this.$API.project.proDic.update.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				if(this.mode=="edit")
				{
					this.form.sortNo = data.sortNo
				}else
				{
					this.form.sortNo = 0
				}
				this.form.dicId = data.dicId
				this.form.name = data.name
				this.form.code = data.code
				this.form.keyValue = data.keyValue
				this.form.status = data.status
				this.form.parentId = data.parentId
			}
		}
	}
</script>

<style>
</style>
